package com.app.bimo.module_bookrack.mvp.persenter;




import com.app.bimo.commonbusiness.mvp.BasePresenter;
import com.app.bimo.commonbusiness.network.RxObservableUtil;
import com.app.bimo.library_db.BookData;
import com.app.bimo.library_db.UserData;
import com.app.bimo.library_db.helper.BookHelper;
import com.app.bimo.library_db.helper.UserHelper;
import com.app.bimo.module_bookrack.mvp.constract.BookrackDelContract;

import java.util.List;

import io.reactivex.Observable;


public class BookrackDelPresenter extends BasePresenter<BookrackDelContract.Model, BookrackDelContract.View> {
    public BookrackDelPresenter(BookrackDelContract.Model model, BookrackDelContract.View rootView) {
        super(model, rootView);
    }


    /**
     * 拿本地  或者网络上的书籍数据
     *
     * @param page
     */

    public void requestFromModel(int page) {

        String uuid = "";
        UserData userData = UserHelper.getsInstance().findUser();
        if (userData != null) {
            uuid = userData.getUuid();
        }
        Observable<List<BookData>> observable;
        if (page == 1) {
            observable = RxObservableUtil.concat(BookHelper.getsInstance().localObservable(uuid), RxObservableUtil.composeHaveCache(mModel.getBooks(page + "")));
        } else {
            observable = RxObservableUtil.composeHaveCache(mModel.getBooks(page + ""));
        }

//        RxObservableUtil.subscribeWithOutCompose(
//                observable.map(this::add)
//                , bindCustomLoadingLifecycle(false), bookStoreDataList -> {
//                    // 处理数据 直接获取到List<JavaBean> carBeans
////                        mRootView.showEmpty();
//                        mRootView.channelDataNotify(bookStoreDataList);
//                });
    }

    /***
     * 不管有没有存数据库 都把书籍拿出来放进数据库
     * 如果有登录  添加当前用户数据进去
     * @param list
     * @return
     */
    private List del(List<BookData> list) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setInBookself(0);
        }
        BookHelper.getsInstance().delBooksToBookrack(list);
        return list;
    }

    /***
     * 不管有没有存数据库 都把书籍拿出来放进数据库
     * 如果有登录  添加当前用户数据进去
     * @param list
     * @return
     */
    private List add(List<BookData> list) {
//        UserData userData = UserHelper.getsInstance().findUser();
//        for (int i = 0; i < list.size(); i++) {
//            list.get(i).setInBookself(1);
//            if (userData != null) {
//                list.get(i).setUuid(userData.getUuid());
//            }
//        }
//        BookHelper.getsInstance().saveBooksToBookRack(list,userData.getUuid());
        return list;
    }


    /**
     * 移除书架
     *
     * @param list
     */
    public void delRequestFromModel(List<BookData> list) {
        String ids = "";
        for (int j = 0; j < list.size(); j++) {
            ids = ids + list.get(j).getNovelid() + ",";
        }
        ids = ids.substring(0, ids.length() - 1);
        RxObservableUtil.subscribe(
                mModel.delBook(ids)
                , bindCustomLoadingLifecycle(true), bookStoreDataList -> {
                    // 处理数据 直接获取到List<JavaBean> carBeans
                    del(list);

                    mRootView.delDataNotify();
                });
    }


}
